//index.js
//获取应用实例
const app = getApp()
const api = require('../../utils/request')
const utils = require('../../utils/util')
Page({
  data: {
    loading: true,
    userInfo: {},
    hasUserInfo: false,
    banners: [], // 轮播广告
    navList: [], // 分类
    goodsList: [],
    hotList:[],
    newList:[],
    activeNav: 0,
    listQuery: {
      page: 1,
      limit: 20,
      idCategory: undefined
    }
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    // if (app.globalData.userInfo) {
    //   this.setData({
    //     userInfo: app.globalData.userInfo,
    //     hasUserInfo: true
    //   })
    // } else if (this.data.canIUse){
    //   // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //   // 所以此处加入 callback 以防止这种情况
    //   app.userInfoReadyCallback = res => {
    //     this.setData({
    //       userInfo: res.userInfo,
    //       hasUserInfo: true
    //     })
    //   }
    // } else {
    //   // 在没有 open-type=getUserInfo 版本的兼容处理
    //   wx.getUserInfo({
    //     success: res => {
    //       app.globalData.userInfo = res.userInfo
    //       this.setData({
    //         userInfo: res.userInfo,
    //         hasUserInfo: true
    //       })
    //     }
    //   })
    // }

    this.getCategoryBanner();
    this.getHotList();
    this.getNewSearch();
  },
  getHotList() {
    api.get('/goods/searchHot').then(res => {
      let goodsList = res
      for (let i = 0; i < goodsList.length; i++) {
        goodsList[i].price = utils.formatPrice(goodsList[i].price)
        goodsList[i].imgUrl = utils.fileMgrUrl + goodsList[i].pic
      }
      this.setData({
        hotList: goodsList
      })
    })
  },
  getNewSearch() {
    api.get('/goods/searchNew').then(res => {
      let goodsList = res
      for (let i = 0; i < goodsList.length; i++) {
        goodsList[i].price = utils.formatPrice(goodsList[i].price)
        goodsList[i].imgUrl = utils.fileMgrUrl + goodsList[i].pic
      }
      this.setData({
        newList: goodsList
      })
      console.log(goodsList)
    })
  },
  getCategoryBanner(){
    api.get('/category/list').then(res=>{
      this.setData({
        navList: res,
        loading: false
      })
      const banner = this.processBanners(res);
      console.log(banner)
      this.setData({
        banners:banner,
        listQuery: Object.assign({},this.data.listQuery,{idCategory:res[0].id})
      })
      this.getGoodsList()
    })
  },
  processBanners(data) {
    const banners = data.reduce((arr,cur)=>{
      const b = cur.bannerList;
      if(b && b.length>0){
        b.forEach((item)=>{
          arr.push(item)
        })
      }
      return arr;
    },[])
    for (let i = 0; i < banners.length; i++) {
      let url = ''
      let page = banners[i].page
      let param = banners[i].param

      if (!utils.startWith(page, 'http')) {
        url = '../' + page + '/index'
      }
      if (param !== '') {
        const paramJson = JSON.parse(param)
        let i = 0
        for (const key in paramJson) {
          if (i === 0) {
            url += '?' + key + '=' + paramJson[key]
          } else {
            url += '&' + key + '=' + paramJson[key]
          }
          i++
        }
      }
      banners[i].url = url
      banners[i].imgUrl = utils.fileMgrUrl + banners[i].idFile
    }
    return banners
  },
  // 获取不同分类商品列表
  getGoodsList() {
    console.log( this.data.listQuery)
    // api.get('/goods/queryGoods?page=' + this.data.listQuery.page + '&limit=' + this.data.listQuery.limit + '&idCategory=' + this.data.listQuery.idCategory, this.listQuery).then(res => {
    //   let goodsList = res.data.records
    //   for (let i = 0; i < goodsList.length; i++) {
    //     goodsList[i].price = utils.formatPrice(goodsList[i].price)
    //     goodsList[i].imgUrl = utils.fileMgrUrl + goodsList[i].pic
    //   }
    //   this.setData({
    //     goodsList: goodsList
    //   })
    // })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  gotoGoodsDetail(event){
    let id = event.currentTarget.dataset.gid;
    wx.navigateTo({
      url: '/pages/goods/index?id='+id,
    })
  }
})
